<template>
  <section class="paper-list">
    <paper-edit :visible.sync="dialogEditVisible" @listPage="listPage"></paper-edit>
    <paper-view :visible.sync="dialogViewVisible" :paper="paper"></paper-view>
    <el-button type="primary" round @click="dialogEditVisible=true">新建试卷</el-button>
    <el-table :data="tableData" stripe height="calc(100vh - 215px)" :style="{marginTop:'10px'}">
      <el-table-column prop="Id" label="ID" width="100">
      </el-table-column>
      <el-table-column prop="Name" label="标题" width="200">
      </el-table-column>
      <el-table-column label="可用" width="60">
        <template slot-scope="scope">
          <i v-if="scope.row.Enable" class="el-icon-success"></i>
          <i v-else class="el-icon-error"></i>
        </template>
      </el-table-column>
      <el-table-column label="板块ID">
        <template slot-scope="scope">
          <span>{{scope.row.Sids}}</span>
        </template>
      </el-table-column>
      <el-table-column label="板块数量" width="80">
        <template slot-scope="scope">
          <span>{{scope.row.Sids.length}}</span>
        </template>
      </el-table-column>
      <el-table-column label="日期" width="240">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span>{{scope.row.CreatedAt}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click.native.prevent="goPaper(scope.row)"
                     type="text" size="small">
            进入试卷<i class="el-icon-d-arrow-right"></i>
          </el-button>
          <el-button @click.native.prevent="viewPaper(scope.row)"
                     type="text" size="small">
            试卷详情<i class="el-icon-d-arrow-right"></i>
          </el-button>
          <el-button @click.native.prevent="del(scope.row)"
                     type="text" size="small">
            删除<i class="el-icon-d-arrow-right"></i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :style="{marginTop:'10px'}"
      @size-change="sizeChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 40, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      background
      @current-change="listPage"
      :total="total">
    </el-pagination>
  </section>
</template>

<script>
import {PaperService} from '@/service/api'
import EventBus from '@/service/event'
import PaperEdit from './Edit'
import PaperView from './View'

export default {
  components: {
    PaperView,
    PaperEdit
  },
  data: function () {
    return {
      total: 0,
      tableData: [],
      paper: {},
      pageSize: 10,
      currentPage: 1,
      dialogEditVisible: false,
      dialogViewVisible: false
    }
  },
  mounted: function () {
    this.listPage()
    EventBus.$emit('acticePath', 'paper')
  },
  methods: {
    sizeChange: function (val) {
      this.pageSize = val
      this.listPage()
    },
    listPage: function (val) {
      PaperService.page(val, this.pageSize)
        .then(res => {
          if (res) {
            this.tableData = res.papers
            this.total = res.total
          }
        })
    },
    goPaper: function (item) {
      this.$router.push({path: '/section', query: {pid: item.Id, pname: item.Name}})
    },
    viewPaper: function (item) {
      this.paper = item
      this.dialogViewVisible = true
    },
    del: function (item) {
      // todo delete
    }
  }
}
</script>

<style scoped>
</style>
